package com.example.hybridmobiledemo.view;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.hybridmobiledemo.R;
import com.example.hybridmobiledemo.module.main.bean.NewsInfo;
import com.example.hybridmobiledemo.module.main.model.news.content.NewsConActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.example.hybridmobiledemo.module.main.fragment.MainFragment.getImgList;

/**
 * @ClassName: NewsRvAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 21/10/2021$
 * @Version: 1.0
 */
public class NewsRvAdapter extends RecyclerView.Adapter<NewsRvAdapter.ViewHolder>{
    private List<NewsInfo> list;
    private Context context;
    private ArrayList<String> imgUrls = new ArrayList<String>();

    public NewsRvAdapter(List<NewsInfo> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_newslist, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NewsInfo info = list.get(position);
        int id = info.getId();
        String title = info.getName();
        String author = info.getAuthor();
        String time = TimeStamp2Date(String.valueOf(info.getCreateTime()), "yyyy-MM-dd HH:mm");
        String content = info.getContent();
        String url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.soutu123.com%2Felement_origin_min_pic%2F01%2F37%2F92%2F40573c69065b76e.jpg%21%2Ffw%2F700%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue&refer=http%3A%2F%2Fpic.soutu123.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637479145&t=53e6f4d78b01d7539df425dc3b6960be";
        String con = "标题内容";

        List<String> urls = getImgList(content, "http://", ".jpg");

        for (String u : urls) {
            url = u.substring(0, 80);
            break;
        }
        List<String> contents = getImgList(content, ">", "。</");
        for (String c : contents) {
            con = c.substring(1, c.length()-3);
            break;
        }

        holder.tvRvNewsContent.setText(con);
        holder.tvRvNewsTitle.setText(title);
        holder.tvRvNewsTime.setText(time);
        holder.tvRvNewsAuthor.setText(author);
        Glide.with(context).load(url).into(holder.imgRvNews);
        holder.linearRvNews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NewsConActivity.class);
                intent.putExtra("SCNewsId", id);
                intent.putExtra("conUrl", content);
                intent.putExtra("title", title);
                intent.putExtra("author", author);
                intent.putExtra("time", time);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

//  时间戳转为时间
    public static String TimeStamp2Date(String timestampString, String formats) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
        return date;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imgRvNews;
        TextView tvRvNewsTitle, tvRvNewsContent, tvRvNewsAuthor, tvRvNewsTime;
        LinearLayout linearRvNews;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.linearRvNews = itemView.findViewById(R.id.linearRvNews);
            this.imgRvNews = itemView.findViewById(R.id.imgRvNews);
            this.tvRvNewsTitle = itemView.findViewById(R.id.tvRvNewsTitle);
            this.tvRvNewsContent = itemView.findViewById(R.id.tvRvNewsContent);
            this.tvRvNewsAuthor = itemView.findViewById(R.id.tvRvNewsAuthor);
            this.tvRvNewsTime = itemView.findViewById(R.id.tvRvNewsTime);
        }
    }
}
